/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/
/*
Authors: Ram Ovadia 201374022 and Lior Fass 300069705
Project: HW Assignment 4 
Description: This is the header of the module implementing the roadmap object of Oiway.  
*/
/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/

#ifndef MAP_H
#define MAP_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common_utils.h"

//macro to help abstract a 1d array as a 2d matrix. 
#define GRAPH(u,v) map->graph[u*(map->n) + v]

/*
 * struct to represent a node on the graph. 
 * mappes signed int coordinates x,y to node index
 */
typedef struct node_t
{
	int x;
	int y;
	int ind;
} Node;

/*
 * struct to represent a roadmap.
 * n - number of nodes on graph. 
 * graph - pointer to 2d matrix holding the adjacency matrix representing the roadmap. 
 * map_index - array of nodes, relating the nodes indexes to coordinates. 
 */
typedef struct map_t
{
	int n;
	int *graph;
	Node *map_index;
} Map;


/*
 * Initiates a map struct. Allocates space and initiates variables.    
 * 
 * Input:
 * -----
 * n - number of nodes required in the graph.      
 *
 * Output:
 * -----
 * succesful - pointer to newly allocated map structure. map->graph, map->map_index initialized. 
 * failed - returns NULL;
 */ 
Map *map_init(int n);

/*
 * Cleans a map struct. deallocates space and frees variables.    
 * 
 * Input:
 * -----
 * map - pointer to Map struct.      
 *
 * Output:
 * -----
 * void
 */ 
void map_clean(Map *map);

/*
 * marshals (serializes) map struct.    
 * 
 * Input:
 * -----
 * map - pointer to Map struct.      
 *
 * Output:
 * -----
 * pointer to buffer holding serialized map struct. must be freed. Null if fails. 
 */ 
char* map_marshal(Map *map);

/*
 * unmarshals (unserializes) map struct. initiates a new map struct and returns it.     
 * 
 * Input:
 * -----
 * buf - pointer to buffer holding serialized Map struct.      
 *
 * Output:
 * -----
 * pointer to Map struct holding unserialized map struct in buffer. must be freed. Null if fails. 
 */ 
Map *map_unmarshal(char *buf);

/*
 * read the graph file to buffer, send it to map Unmarshal, and return allocated Map
 *
 * Input:
 * -----
 * graphTxt - string containing the file path
 *
 * Output:
 * -----
 * pointer to Map, this memory should be cleaned using map_clean
 * NULL - failed. 
 */
Map *LoadMapFromFile(char *graphTxt);

/*
 * Parse recieved string from user, calculate new weight, update map and print to log
 *
 * Input:
 * -----
 * recievedStr - array containing the recieved string
 * map - pointer to the map, the map is locked during the run of this function
 * log_file - output log file
 * Ind - the index of the calling thread for printing to log
 *
 * Output:
 * -----
 * 0 - successful. map updated, log printed. 
 */
int UpdateMap(char* recievedStr, Map *map, FILE *log_file, int Ind);

/*
 * prints the Map to screen and file, should be called at end of the run
 *
 * Input:
 * -----
 * map - pointer to the map.
 * file - file handle for the log file
 *
 * Output:
 * -----
 * prints the map to stdout and log file
 */
void map_print(Map *map,FILE *file);

/*
 * translates index of node in map to coordinates in node   
 * 
 * Input:
 * -----
 * map - pointer to map struct.
 * ind - index of node in map. 
 *
 * Output:
 * -----
 * successful - pointer to node of index ind in map_index
 * unsuccessful - NULL. 
 */ 
Node *map_i2c(Map *map, int ind);

/*
 * translates coordinates of node in graph to the nodes index.     
 * 
 * Input:
 * -----
 * map - pointer to map struct.
 * mode - pointer to node holding the coordinates to be translated.  
 *
 * Output:
 * -----
 * successful - index of node in map. 
 * unsuccessful - -1. 
 */ 
int map_c2i(Map *map, Node *node);

/*
 * compares to nodes for equality in coordinates. index is unattended.
 * 
 * Input:
 * -----
 * node1, node2 - pointers to nodes to be compared.  
 *
 * Output:
 * -----
 * 0 - successful  
 * 1 - unsuccessful 
 */
int map_nodecmp(Node *node1, Node *node2);

#endif // MAP_H